﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace YXShop.Web.admin.templates.lable
{
    public partial class labelclass_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011002002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("011002004", "对不起，您没有权限进行编辑");
                this.bindTemplateStyle();
                if (ChangeHope.WebPage.PageRequest.GetQueryInt("id")>0)
                {
                    bindInfo(ChangeHope.WebPage.PageRequest.GetQueryInt("id"));
                }
                
                InitWebControl();
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("templatepath");
            string path = Server.MapPath("~//" + templatePath + "/label/labeltype.xml");
            if (!File.Exists(path))
            {
                StringBuilder shtml = new StringBuilder();
                shtml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
                shtml.Append("<labeltypegroup>\r\n");
                shtml.Append("</labeltypegroup>\r\n");
                string fileContent = shtml.ToString();
                ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
                fh.CreateFile(path, fileContent);
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            if (ViewState["ID"] == null)
            {
                XmlNode root = xmlDoc.SelectSingleNode("labeltypegroup");//查找<labeltypegroup>
                int id = 1;
                if (root.ChildNodes.Count > 0)
                {
                    id = int.Parse(root.LastChild.Attributes.GetNamedItem("id").Value) + 1;
                }
                XmlElement xe1 = xmlDoc.CreateElement("labeltype");//创建一个<labeltype>节点
                xe1.SetAttribute("id", id.ToString());//设置该节点genre属性
                xe1.SetAttribute("isstorelabel", "0");//设置该节点ISBN属性
                XmlElement xesub1 = xmlDoc.CreateElement("name");
                xesub1.InnerText = this.txtName.Text.Trim();//设置文本节点
                xe1.AppendChild(xesub1);//添加到<labeltype>节点中
                XmlElement xesub2 = xmlDoc.CreateElement("description");
                xesub2.InnerText = this.txtDescription.Text.Trim();
                xe1.AppendChild(xesub2);
                XmlElement xesub3 = xmlDoc.CreateElement("creattime");
                xesub3.InnerText = System.DateTime.Now.ToString();
                xe1.AppendChild(xesub3);
                XmlElement xesub4 = xmlDoc.CreateElement("styleid");
                xesub4.InnerText = this.ddlStyle.SelectedValue;
                xe1.AppendChild(xesub4);
                root.AppendChild(xe1);//添加到<labeltype>节点中
            }
            else
            {
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("labeltypegroup").ChildNodes;//获取labeltype节点的所有子节点
                foreach (XmlNode xn in nodeList)//遍历所有子节点
                {
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                    if (xe.GetAttribute("id") == ViewState["ID"].ToString())
                    {
                        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                        foreach (XmlNode xn1 in nls)//遍历
                        {
                            XmlElement xe2 = (XmlElement)xn1;//转换类型
                            if (xe2.Name == "name")//如果找到
                            {
                                xe2.InnerText = this.txtName.Text.Trim();//则修改
                                continue;//找到退出来就可以了
                            }
                            if (xe2.Name == "description")//如果找到
                            {
                                xe2.InnerText = this.txtDescription.Text.Trim();//则修改
                                continue;//找到退出来就可以了
                            }
                            if (xe2.Name == "creattime")//如果找到
                            {
                                xe2.InnerText = System.DateTime.Now.ToString();//则修改
                                continue;//找到退出来就可以了
                            }
                            if (xe2.Name == "styleid")//如果找到
                            {
                                xe2.InnerText = this.ddlStyle.SelectedValue;//则修改
                                continue;//找到退出来就可以了
                            }
                        }
                        break;
                    }
                }

            }
            xmlDoc.Save(path);



            //YXShop.BLL.Templates.LabelType bll = new YXShop.BLL.Templates.LabelType();
            //YXShop.Model.Templates.LabelType model = new YXShop.Model.Templates.LabelType();
            //model.Name = this.txtName.Text.Trim();
            //model.Description = this.txtDescription.Text.Trim();
            //model.CreatTime = System.DateTime.Now;
            //model.IsStoreLabel = Convert.ToInt32(ViewState["IsStoreLabel"].ToString());
            //model.StyleId = Convert.ToInt32(this.ddlStyle.SelectedValue);
            if (ViewState["ID"] != null)
            {
                //model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                //if (bll.Update(model) != 0)
                //{
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                //}
            }
            else
            {
                //if (bll.Add(model) != 0)
                //{
                    this.txtName.Text = "";
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                //}
            }
        }

        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("templatepath");
            ViewState["IsStoreLabel"] = ChangeHope.WebPage.PageRequest.GetQueryString("IsStoreLabel");
            this.HyperLink1.NavigateUrl = "labelclass_list.aspx?q_templatepath=" + templatePath + "&w_d_isstorelabel=" + ViewState["IsStoreLabel"].ToString();
            this.HyperLink2.NavigateUrl = "labelclass_list.aspx?q_templatepath=" + templatePath + "&w_d_isstorelabel=" + ViewState["IsStoreLabel"].ToString();
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入标签分类的名称,您可以对标签进行归类", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }

        protected void bindInfo(int id)
        {
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("templatepath");
            string path = Server.MapPath("~//" + templatePath + "/label/labeltype.xml");
            ViewState["ID"] = id;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("labeltypegroup").ChildNodes;//获取labeltype节点的所有子节点
            foreach (XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                if (xe.GetAttribute("id") == id.ToString())
                {
                    XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                    foreach (XmlNode xn1 in nls)//遍历
                    {
                        XmlElement xe2 = (XmlElement)xn1;//转换类型
                        if (xe2.Name == "name")//如果找到
                        {
                            this.txtName.Text = xe2.InnerText;
                            continue;//找到退出来就可以了
                        }
                        if (xe2.Name == "description")//如果找到
                        {
                            this.txtDescription.Text = xe2.InnerText;
                            continue;//找到退出来就可以了
                        }

                        if (xe2.Name == "styleid")//如果找到
                        {
                            this.ddlStyle.SelectedValue = xe2.InnerText;
                            continue;//找到退出来就可以了
                        }
                    }
                    break;
                }
            }



            //YXShop.BLL.Templates.LabelType bll = new YXShop.BLL.Templates.LabelType();
            //YXShop.Model.Templates.LabelType model = bll.GetModel(id);
            //if (model != null)
            //{
            //    this.ddlStyle.SelectedValue = model.StyleId.ToString();
            //    this.txtName.Text = model.Name;
            //    this.txtDescription.Text = model.Description;
            //    ViewState["ID"] = model.ID;
            //}
        }

        protected void bindTemplateStyle()
        {
            YXShop.BLL.SystemInfo.ShopStyle bll = new YXShop.BLL.SystemInfo.ShopStyle();
            List<YXShop.Model.SystemInfo.ShopStyle> list = bll.GetColList();
            if (list.Count > 0)
            {
                foreach (YXShop.Model.SystemInfo.ShopStyle ss in list)
                {
                    this.ddlStyle.Items.Add(new ListItem(ss.Name, ss.ID.ToString()));
                }
            }
        }
    }
}
